package com.isnotlocus.gulimail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.isnotlocus.gulimail.product.dao.AttrAttrgroupRelationDao;
import com.isnotlocus.gulimail.product.entity.AttrAttrgroupRelationEntity;
import com.isnotlocus.gulimail.product.entity.AttrEntity;
import com.isnotlocus.gulimail.product.entity.ProductAttrValueEntity;
import com.isnotlocus.gulimail.product.service.*;
import com.isnotlocus.gulimail.product.vo.AttrGroupAndAttrVo;
import com.isnotlocus.gulimail.product.vo.AttrRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.isnotlocus.gulimail.product.entity.AttrGroupEntity;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;


/**
 * 属性分组
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 17:00:57
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    @Autowired
    ProductAttrValueService productAttrValueService;


    /**
     * @return
     */
    @GetMapping("{attrGroupId}/attr/relation")
    public R SelectAttrByAttrgourp(@PathVariable("attrGroupId") Long attrGroupId) {
        List<AttrEntity> list = attrService.getAttrByAttrgourp(attrGroupId);

        return R.ok().put("data", list);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catId) {
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long categoryId = attrGroup.getCatelogId();
        Long[] parentId = categoryService.getParentCategoryPath(categoryId);
        attrGroup.setCatelogPath(parentId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        System.out.println("this is save attrGroup ");
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        System.out.println("this is update attrGroup ");
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }



    /**
     * 修改
     * //product
     */
    @PostMapping("/update/{spuId}")
    public R updateBySpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> list) {
        productAttrValueService.updateSpuAtr(spuId,list);

        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除关联关系
     *
     * @param
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrRelationVo[] attrRelationVo) {
        attrService.deleteRelationByAttrIdAndGroupId(attrRelationVo);
        return R.ok("删除成功！！！");
    }

    /**
     * 查询关联属性列表
     *
     * @param AttrGroupId
     * @param map
     * @return
     */
    @GetMapping("/{AttrGroupId}/noattr/relation")
    public R SelectDefaultAttrGroupAttr(@PathVariable("AttrGroupId") Long AttrGroupId, @RequestParam Map<String, Object> map) {
        PageUtils pageUtils = attrService.getNotRelationAttr(AttrGroupId, map);
        System.out.println("=>>>>>>>>>>>>>>>>>>>>>>>>>>dwdwdwdwdwdwd" + map);

        return R.ok().put("page", pageUtils);

    }

    @PostMapping("/attr/relation")
    public R AttrGroupAttrRelation(@RequestBody List<AttrRelationVo> attrRelationVo) {
        boolean save = relationService.saveBatchs(attrRelationVo);
        if (save) {
            return R.ok("添加成功！！！");
        }
        return R.error("添加失败");
    }


    @GetMapping("/{categoryId}/withattr")
    public R AttrGroupAttrRelationByCategoryId(@PathVariable("categoryId") Long categoryId) {
        List<AttrGroupAndAttrVo> data= attrGroupService.SelectAttrGroupMsgAndAttrMsg(categoryId);
        return R.ok().put("data",data);
    }

}
